package com.enjoy.xbase.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.enjoy.xbase.R;

/**
 * 描述：滑动小圆点
 */
public class StripSlide extends View {

    /**
     * 前景画笔
     */
    private Paint mForePaint;
    /**
     * 背景画笔
     */
    private Paint mBgPaint;

    /**
     * 总个数
     */
    private int icount = 5;

    /**
     * 当前位置
     */
    private int postion = 0;

    private int type = 0;


    /**
     * 大小
     */
    private int radius;
    private int slideWidth = 0;
    private int slideHeight = 0;
    private int slideSpan;
    private boolean slideSolid;


    /**
     * 前景色
     */
    private int mForeColor;
    /**
     * 背景色
     */
    private int mBgColor;

    public StripSlide(Context context) {
        super(context);
        initPaint();
    }

    public StripSlide(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Indicator);
        type = typedArray.getInteger(R.styleable.Indicator_slide_type, 1);
        radius = typedArray.getDimensionPixelOffset(R.styleable.Indicator_slide_radius, 30);
        slideWidth = typedArray.getDimensionPixelOffset(R.styleable.Indicator_slide_width, 50);
        slideHeight = typedArray.getDimensionPixelOffset(R.styleable.Indicator_slide_height, 10);
        slideSpan = typedArray.getDimensionPixelOffset(R.styleable.Indicator_slide_span, 10);
        slideSolid= typedArray.getBoolean(R.styleable.Indicator_slide_solid, false);
        mForeColor = typedArray.getColor(R.styleable.Indicator_slide_foreColor, Color.parseColor("#ffffff"));
        mBgColor = typedArray.getColor(R.styleable.Indicator_slide_bgColor, Color.parseColor("#90ffffff"));
        initPaint();
    }

    /**
     * 设置偏移
     *
     * @param position 位置
     */
    public void setOffset(int position) {
        this.postion = position;
        invalidate();
    }

    public void setIcount(int icount) {
        this.icount = icount;
        ViewGroup.LayoutParams lp = getLayoutParams();
        setLayoutParams(lp);
        invalidate();
    }


    private void initPaint() {
        mForePaint = new Paint();
        mForePaint.setAntiAlias(true);
        mForePaint.setStyle(Paint.Style.FILL);
        mForePaint.setColor(mForeColor);
        if (type == 1) {
            mForePaint.setStrokeWidth(slideHeight);
        }
        mForePaint.setStrokeCap(Paint.Cap.ROUND);

        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        if (slideSolid){
            mBgPaint.setStyle(Paint.Style.FILL);
        }else{
            mBgPaint.setStyle(Paint.Style.STROKE);
        }
        mBgPaint.setColor(mBgColor);
        if (type == 1) {//圆
            mBgPaint.setStrokeWidth(slideHeight * 8 / 10);
        }
        mBgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (icount > 0) {
            if (type == 1) {
                int height = getHeight();
                for (int i = 0; i < icount; i++) {
                    //圆心y坐标需要加上strokeWidth/2，因为绘圆绘的是中心圆（半径=外圆半径-strokeWidth/2）
                    //canvas.drawCircle(start + radius*number*i,radius+1,radius,mBgPaint);
                    canvas.drawLine(1 + slideHeight * 8 / 10 + slideWidth * i + slideSpan * i, height / 2 - slideHeight / 2, 1 + slideWidth * (i + 1) + slideSpan * i, height / 2 - slideHeight / 2, mBgPaint);
                }
                canvas.drawLine(1 + slideHeight + slideWidth * postion + slideSpan * postion, height / 2 - slideHeight / 2, 1 + slideWidth * (postion + 1) + slideSpan * postion, height / 2 - slideHeight / 2, mForePaint);
            } else {

                //画多个空心圆,为了使圆不重叠,所以对X轴坐标进行动态的修改
                for (int i = 0; i < icount; i++) {
                    //
                    canvas.drawCircle(1 + radius + i * radius * 2 + i * slideSpan, getHeight() / 2, radius, mBgPaint);
                }
                //画实心圆,为使实心圆能够进行X轴移动   参数1加上了偏移量
                canvas.drawCircle(1 + radius + postion * radius * 2 + postion * slideSpan, getHeight() / 2, radius, mForePaint);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            //小圆点直径 + 画笔宽度（strokeWidth/2 * 2）
            if (type == 1) {
                result = slideHeight * 2 + 2;
            } else {
                result = radius * 2 + 2;
            }
        } else {
            //小圆点直径 + 画笔宽度（strokeWidth/2 * 2）
            if (type == 1) {
                result = slideHeight * 2 + 2;
            } else {
                result = radius * 2 + 2;
            }
        }
        return result;
    }

    private int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            //小圆点总直径 + 圆点间间距 + 画笔宽度（strokeWidth/2 * 2）
            if (type == 1) {
                result = slideWidth * icount + slideSpan * (icount - 1) + 2 + slideHeight;
            } else {
                result = radius * 2 * icount + slideSpan * (icount - 1) + 2;
            }
        } else {
            result = 100;
        }
        return result;
    }
}